//
//  EBMCustomerAlertView.m
//  eBondMall
//
//  Created by 赵世杰 on 2020/3/20.
//  Copyright © 2020 zhaoshijie. All rights reserved.
//

#import "EBMCustomerAlertView.h"

@implementation EBMCustomerAlertView



/**
 初始化方法
 
 @param content 内容
 @param textAlignment 内容字体方向
 @param left 左侧按钮标题
 @param right 右侧按钮标题
 @param leftClickBlock 左侧按钮回调
 @param rightClickBlock 右侧按钮回调
 @return 对象
 */
- (instancetype)initWithContent:(NSString *)content
                textAlignment:(NSTextAlignment)textAlignment
                         left:(NSString *)left
                        right:(NSString *)right
               leftClickBlock:(OperationBlock)leftClickBlock
              rightClickBlock:(OperationBlock)rightClickBlock
{
    self = [super init];
    if(self)
    {
        self.contentString = content;
        self.textAlignment = textAlignment;
        self.leftString = left;
        self.rightString = right;
        self.leftClickBlock = leftClickBlock;
        self.rightClickBlock = rightClickBlock;

        
        //  初始化视图
        [self initView];
    }
    
    return self;
}

#pragma mark ----------初始化视图----------
//  初始化视图
- (void)initView
{
    
    //  创建控件    大小
    [self createViewInFrame];
    
    //  创建控件    背景视图
    [self createViewInBgView];
    
    //  创建控件    内容
    [self createViewInContent];
    
    //  创建控件    按钮
    [self createViewInButton];
    
    
}


//  创建控件    大小
- (void)createViewInFrame
{
    self.frame = [UIScreen mainScreen].bounds;
    
    self.backgroundColor = RGBACOLOR16(@"000000", 0.6);
    
    
    CGSize contentSize = [self.contentString stringComputeSizeWithFont:Font_18_Medium lineSpacing:4 boundWidth:(KScreenWidth*0.8-15*2) boundHeight:1000];
    
    //  高度
    self.contentHight = contentSize.height;
    
    
}

//  创建控件    背景视图
- (void)createViewInBgView
{
    self.bgView = [[UIView alloc]init];
    self.bgView.backgroundColor = [UIColor whiteColor];
    self.bgView.layer.cornerRadius = 5;
    self.bgView.layer.masksToBounds = YES;
    [self addSubview:self.bgView];
    
    //  设置大小
    float selfHeight = (110+self.contentHight);
    self.bgView.frame = CGRectMake(0, 0, KScreenWidth*0.8, selfHeight);
    self.bgView.centerX = KScreenWidth/2.0;
    self.bgView.centerY = KScreenHeight/2.0;
}

//  创建控件    内容
- (void)createViewInContent
{
    self.contentLabel = [[UILabel alloc]init];
    self.contentLabel.font = Font_18_Medium;
    self.contentLabel.textAlignment = self.textAlignment;
    self.contentLabel.textColor = RGBCOLOR16_000000;
    self.contentLabel.text = self.contentString;
    self.contentLabel.numberOfLines = 0;
    [self.bgView addSubview:self.contentLabel];
    
    self.contentLabel.attributedText = [self.contentLabel.text stringAttrWithColor:self.contentLabel.textColor range:NSMakeRange(0, self.contentLabel.text.length) alignment:self.contentLabel.textAlignment lineSpacing:4];
    
    // 约束
    self.contentLabel.translatesAutoresizingMaskIntoConstraints = NO;
    NSDictionary *contentLabel_dic = @{@"contentLabel":self.contentLabel};
    NSString *contentLabel_H = @"H:|-23-[contentLabel]-23-|";
    NSString *contentLabel_V = @"V:|-30-[contentLabel]";
    [self.bgView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:contentLabel_H options:0 metrics:nil views:contentLabel_dic]];
    [self.bgView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:contentLabel_V options:0 metrics:nil views:contentLabel_dic]];
    
    
    
}


//  创建控件    按钮
- (void)createViewInButton
{
    
    UIView *lineView0 = [[UIView alloc]initWithFrame:CGRectMake(0, self.bgView.height-49-0.5, self.bgView.width, 0.5)];
    lineView0.backgroundColor = RGBCOLOR16_E8E8E8;
    [self.bgView addSubview:lineView0];
    
    
    //  宽度
    float buttonWidth = self.bgView.width/2.0;
    
    //  取消
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, self.bgView.height-49, buttonWidth, 49);
    leftButton.titleLabel.font = Font_18;
    [leftButton setTitle:self.leftString forState:UIControlStateNormal];
    [leftButton setTitleColor:RGBCOLOR16_000000 forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(leftButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.bgView addSubview:leftButton];
    
    
    //  确定
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame = CGRectMake(leftButton.right, self.bgView.height-49, buttonWidth, 49);
    rightButton.titleLabel.font = Font_18;
    [rightButton setTitle:self.rightString forState:UIControlStateNormal];
    [rightButton setTitleColor:RGBCOLOR16_576B94 forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(rightButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.bgView addSubview:rightButton];
    
    
    UIView *lineView1 = [[UIView alloc]initWithFrame:CGRectMake(buttonWidth, lineView0.bottom, 0.5, rightButton.height)];
    lineView1.backgroundColor = RGBCOLOR16_E8E8E8;
    [self.bgView addSubview:lineView1];
    
    
    if(!self.leftString)
    {
        //  隐藏左侧按钮
        leftButton.hidden = YES;
        
        rightButton.left = 0;
        rightButton.width = self.bgView.width;
        
        lineView1.hidden = YES;
        
    }
    
}



#pragma mark --------------方法--------------



#pragma mark --------------响应方法--------------

//  取消
- (void)leftButtonClick:(UIButton *)sender
{
    //  移除
    [self removeToWindow];
    
    //  忘记密码回调
    if(self.leftClickBlock)
    {
        self.leftClickBlock();
    }
}

//  确定
- (void)rightButtonClick:(UIButton *)sender
{
    //  移除
    [self removeToWindow];
    
    //  确认回调
    if(self.rightClickBlock)
    {
        self.rightClickBlock();
    }
    
}


@end
